package cn.zhfeat.hatch.web.service.impl.store;

import java.util.Date;
import java.util.List;

import cn.zhfeat.core.base.BaseResultModel;
import cn.zhfeat.core.base.ResultStatus;
import cn.zhfeat.core.shiro.ShiroSessionData;
import cn.zhfeat.hatch.dto.store.BaseStoreInfoDto;
import cn.zhfeat.hatch.dto.store.BaseStoreToUserDto;
import cn.zhfeat.hatch.entity.store.BaseStoreInfo;
import cn.zhfeat.hatch.rpc.IBaseStoreInfoRpcService;
import cn.zhfeat.hatch.web.service.IStoreService;
import cn.zhfeat.hatch.web.utils.ConfigUtil;
import cn.zhfeat.hatch.web.utils.ShiroSessionDataUtil;
import cn.zhfeat.utils.MD5Util;
import cn.zhfeat.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;


/**
 * @description: StoreController对应的Service
 *
 * @author Yangcl
 * @date 2018年5月16日 下午4:55:27
 * @version 1.0.0.1
 */
@Service("storeService")
public class StoreServiceImpl implements IStoreService {
	
	@Autowired
	private IBaseStoreInfoRpcService baseStoreInfoRpcService;

	/**
	 * @description: 查询门店列表
	 *
	 * @param dto
	 * @author ZhangHao
	 * @date 2018年5月16日 下午4:58:36
	 * @version 1.0.0.1
	 */
	public BaseResultModel ajaxStorePageList(BaseStoreInfoDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setShiroSessionData(shiroSessionData);
		dto.setCid(shiroSessionData.getCid()); 
		if(dto.getCid() == null) {
			baseResultModel.setStatus(ResultStatus.CID_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.CID_ERROR.getStatusMessage());
			return baseResultModel;
		}
		PageInfo<BaseStoreInfo> ajaxPageList = baseStoreInfoRpcService.ajaxPageList(dto);
		if (ajaxPageList == null || ajaxPageList.getList().size() == 0) {
			baseResultModel.setStatus(ResultStatus.PAGE_LIST_NULL.getStatusCode());
			baseResultModel.setMessage(ResultStatus.PAGE_LIST_NULL.getStatusMessage());
			return baseResultModel;
		}

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		baseResultModel.setData(ajaxPageList.getList());
		baseResultModel.setTotalCount(Integer.parseInt(String.valueOf(ajaxPageList.getTotal())));
		return baseResultModel;
	}

	/**
	 * @description: 根据dto获取门店列表 原方法名：findByName
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月18日 下午2:31:59
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel findListByDto(BaseStoreInfoDto dto) {
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setShiroSessionData(shiroSessionData);
		dto.setCid(shiroSessionData.getCid()); 
		BaseResultModel baseResultModel = new BaseResultModel();
		List<BaseStoreInfo> list = baseStoreInfoRpcService.findListByDto(dto);
		if (list != null && list.size() > 0) {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
			baseResultModel.setData(list);
		} else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}

	/**
	 * 
	 * @Description: 查询门店总数
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月21日 下午7:36:58
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxStoreCount() {

		BaseResultModel model = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		BaseStoreInfoDto baseStoreInfoDto = new BaseStoreInfoDto();
		baseStoreInfoDto.setCid(shiroSessionData.getCid());
		Integer count = baseStoreInfoRpcService.selectCountByType(baseStoreInfoDto);
		if (count == null || count == 0) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		model.setData(count);
		return model;
	}

	/**
	 * 
	 * @Description: 添加门店
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月21日 下午7:36:58
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxSaveStore(BaseStoreInfoDto baseStoreInfoDto) {
		BaseResultModel model = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		Long cid = shiroSessionData.getCid();
		if (baseStoreInfoDto == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		baseStoreInfoDto.setCid(cid);
		// 查询当前公司门店总数
		Integer storeCount = baseStoreInfoRpcService.selectCountByType(baseStoreInfoDto);
		if (storeCount != null && storeCount > 500) {
			model.setStatus(ResultStatus.ENTITY_EXIT.getStatusCode());
			model.setMessage("门店数量请勿超过500个");
			return model;
		}
		
        /** wschenyingyin修改**/
		BaseStoreInfo storeInfo = baseStoreInfoRpcService.findEntityByType(baseStoreInfoDto);
		if (storeInfo != null) {
			model.setStatus(ResultStatus.ENTITY_EXIT.getStatusCode());
			model.setMessage("该门店已存在，请勿重复创建");
			return model;
		}
		// 门店实体转换
		BaseStoreInfo baseStoreInfo = convertBaseStoreInfo(baseStoreInfoDto);
		// 密码加密
		if (!StringUtils.isBlank(baseStoreInfoDto.getPwd())) {
			String passMd5 = MD5Util
					.encodeString(MD5Util.encodeString(baseStoreInfoDto.getPwd()) + ConfigUtil.MD5_PWD_STR);
			baseStoreInfo.setPwd(passMd5);
		}
		// 保存门店信息
		baseStoreInfo = baseStoreInfoRpcService.insertSelective(baseStoreInfo);
		if (baseStoreInfo == null) {
			model.setStatus(ResultStatus.INSERT_ERROR.getStatusCode());
			model.setMessage(ResultStatus.INSERT_ERROR.getStatusMessage());
			return model;
		}
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}

	/**
	 * 
	 * @Description: 门店实体转换
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月21日 下午8:43:43
	 * @version 1.0.0.1
	 * @param baseStoreInfoDto
	 */
	private BaseStoreInfo convertBaseStoreInfo(BaseStoreInfoDto baseStoreInfoDto) {
		BaseStoreInfo baseStoreInfo = new BaseStoreInfo();
		baseStoreInfo.setName(baseStoreInfoDto.getName());
		baseStoreInfo.setCid(baseStoreInfoDto.getCid());
		baseStoreInfo.setOrganizationCode(baseStoreInfoDto.getOrganizationCode());
		baseStoreInfo.setOrganizationImg(baseStoreInfoDto.getOrganizationImg());
		baseStoreInfo.setDeleteTime(baseStoreInfoDto.getDeleteTime());
		baseStoreInfo.setCityId(baseStoreInfoDto.getCityId());
		baseStoreInfo.setCreateUserId(baseStoreInfoDto.getCreateUserId());
		baseStoreInfo.setIsDeleted(baseStoreInfoDto.getIsDeleted());
		baseStoreInfo.setProvinceId(baseStoreInfoDto.getProvinceId());
		baseStoreInfo.setStoreCode(baseStoreInfoDto.getStoreCode());
		baseStoreInfo.setCreateTime(baseStoreInfoDto.getCreateTime());
		baseStoreInfo.setAddress(baseStoreInfoDto.getAddress());
		baseStoreInfo.setId(baseStoreInfoDto.getId());
		baseStoreInfo.setUpdateTime(new Date());
		baseStoreInfo.setPhone(baseStoreInfoDto.getPhone());
		baseStoreInfo.setLng(baseStoreInfoDto.getLng());
		baseStoreInfo.setLat(baseStoreInfoDto.getLat());
		baseStoreInfo.setAreaId(baseStoreInfoDto.getAreaId());
		return baseStoreInfo;
	}

	/**
	 * 
	 * @Description: 根据条件查询门店
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月21日 下午7:36:58
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxQueryStore(BaseStoreInfoDto baseStoreInfoDto) {
		BaseResultModel model = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		if (baseStoreInfoDto == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		baseStoreInfoDto.setCid(shiroSessionData.getCid());
		// 查询门店
		BaseStoreInfo baseStoreInfo = baseStoreInfoRpcService.findEntityByType(baseStoreInfoDto);
		if (baseStoreInfo == null) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		model.setData(baseStoreInfo);
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}

	/**
	 * 
	 * @Description: 删除门店
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月22日 上午9:38:34
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxDeleteStore(BaseStoreInfoDto baseStoreInfoDto) {

		BaseResultModel model = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		Long cid = shiroSessionData.getCid();
		Long userId = shiroSessionData.getUserId();
		if (baseStoreInfoDto == null || baseStoreInfoDto.getId() == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		// 删除门店信息
		BaseStoreInfo storeInfo = new BaseStoreInfo();
		storeInfo.setId(baseStoreInfoDto.getId());
		storeInfo.setUpdateTime(new Date());
		storeInfo.setDeleteTime(new Date());
		storeInfo.setIsDeleted(1);
		storeInfo.setCid(cid);
		Integer result = baseStoreInfoRpcService.updateSelective(storeInfo);
		if (result != 1) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return model;
		}
		//修改门店和用户对应关系
		BaseStoreToUserDto storeToUserDto = new BaseStoreToUserDto();
		storeToUserDto.setCid(cid);
		storeToUserDto.setUserId(userId);
		storeToUserDto.setStoreId(baseStoreInfoDto.getId());
		storeToUserDto.setIsDeleted(1);
		storeToUserDto.setUpdateTime(new Date());
		storeToUserDto.setDeleteTime(new Date());
	    baseStoreInfoRpcService.updateBaseStoreToUserByType(storeToUserDto);
	    
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}

	/**
	 * 
	 * @Description: 编辑门店
	 * 
	 * @author: ZhangHao
	 * @date: 2018年5月22日 上午9:38:34
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxUpdateStore(BaseStoreInfoDto baseStoreInfoDto) {
		BaseResultModel model = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		Long cid = shiroSessionData.getCid();
		if (baseStoreInfoDto == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		if("总部".equals(baseStoreInfoDto.getName())) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage("总部门店不允许编辑!");
			return model;
		}
		baseStoreInfoDto.setCid(cid);
		//查询相同门店数量
		Integer count = baseStoreInfoRpcService.getCountByType(baseStoreInfoDto);
		if(count != null && count > 0) {
			model.setStatus(ResultStatus.ENTITY_EXIT.getStatusCode());
			model.setMessage("该门店已存在!");
			return model;
			     
		}
		//数据转换   
		BaseStoreInfo baseStoreInfo = convertBaseStoreInfo(baseStoreInfoDto);
		// 门店密码加密
		if (!StringUtils.isBlank(baseStoreInfoDto.getPwd()) && !"abcdef".equals(baseStoreInfoDto.getPwd())) {
			String passMd5 = MD5Util
					.encodeString(MD5Util.encodeString(baseStoreInfoDto.getPwd()) + ConfigUtil.MD5_PWD_STR);
			baseStoreInfo.setPwd(passMd5);
		} else {
			baseStoreInfo.setPwd("");
		}
		Integer result = baseStoreInfoRpcService.updateSelective(baseStoreInfo);
		if (result != 1) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return model;
		}
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}

	/**
	 * @description: 获取企业下的全部门店，不分页
	 *
	 * @param baseStoreInfoDto
	 * @return 
	 * @author ZhangHao
	 * @date 2018年5月27日 上午2:56:19 
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxSelectAllStore(BaseStoreInfoDto baseStoreInfoDto) {
		BaseResultModel baseResultModel = new BaseResultModel();
 		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		baseStoreInfoDto.setCid(shiroSessionData.getCid());
		List<BaseStoreInfo> lists = baseStoreInfoRpcService.findListByDto(baseStoreInfoDto);
		if(lists != null && lists.size() > 0) {
			baseResultModel.setData(lists);
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.HTTP_REQUEST_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.HTTP_REQUEST_ERROR.getStatusMessage());
		}
		return baseResultModel;
	}

}